package com.zerowidth.db.op;

import com.zerowidth.db.DBHelper;
import com.zerowidth.db.base.SqlResult;
import com.zerowidth.db.bean.FreezeUserBean;

import java.sql.SQLException;

public class FreezeUserDB {

    public static FreezeUserBean findFreeze(String phoneNumber) {
        String sql = String.format("SELECT * FROM freeze_user WHERE phone_number='%s'" +
                " AND enable = 1", phoneNumber);

        SqlResult sr = DBHelper.executeQueryEx(sql);
        if (sr == null) {
            return null;
        }
        if (sr.rs == null) {
            sr.close();
            return null;
        }

        try {
            if (sr.rs.next()) {
                FreezeUserBean bean = new FreezeUserBean();
                bean.id = sr.rs.getInt("id");
                bean.phone_number = sr.rs.getString("phone_number");
                bean.reason = sr.rs.getString("reason");
                bean.free_time = sr.rs.getString("free_time");
                bean.create_time = sr.rs.getString("create_time");
                bean.update_time = sr.rs.getString("update_time");
                bean.enable = sr.rs.getBoolean("enable");

                return bean;
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }

        return null;
    }

    public static FreezeUserBean findFreezeWXid(String wxunionId) {
        String sql = String.format("SELECT * FROM freeze_user WHERE wxunionid='%s'" +
                " AND enable = 1", wxunionId);

        SqlResult sr = DBHelper.executeQueryEx(sql);
        if (sr == null) {
            return null;
        }
        if (sr.rs == null) {
            sr.close();
            return null;
        }

        try {
            if (sr.rs.next()) {
                FreezeUserBean bean = new FreezeUserBean();
                bean.id = sr.rs.getInt("id");
                bean.phone_number = sr.rs.getString("phone_number");
                bean.reason = sr.rs.getString("reason");
                bean.free_time = sr.rs.getString("free_time");
                bean.create_time = sr.rs.getString("create_time");
                bean.update_time = sr.rs.getString("update_time");
                bean.enable = sr.rs.getBoolean("enable");

                return bean;
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }

        return null;
    }

    public static FreezeUserBean findFreezeByUserId(String userId) {
        String sql = String.format("SELECT * FROM freeze_user WHERE freeze_user_id='%s'" +
                " AND enable = 1", userId);

        SqlResult sr = DBHelper.executeQueryEx(sql);
        if (sr == null) {
            return null;
        }
        if (sr.rs == null) {
            sr.close();
            return null;
        }

        try {
            if (sr.rs.next()) {
                FreezeUserBean bean = new FreezeUserBean();
                bean.id = sr.rs.getInt("id");
                bean.phone_number = sr.rs.getString("phone_number");
                bean.userId = sr.rs.getString("freeze_user_id");
                bean.reason = sr.rs.getString("reason");
                bean.free_time = sr.rs.getString("free_time");
                bean.create_time = sr.rs.getString("create_time");
                bean.update_time = sr.rs.getString("update_time");
                bean.enable = sr.rs.getBoolean("enable");

                return bean;
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }

        return null;
    }

    public static FreezeUserBean findFreezeByStatId(String statId) {
        String sql = String.format("SELECT * FROM freeze_user WHERE freeze_stat_id='%s'" +
                " AND enable = 1", statId);

        SqlResult sr = DBHelper.executeQueryEx(sql);
        if (sr == null) {
            return null;
        }
        if (sr.rs == null) {
            sr.close();
            return null;
        }

        try {
            if (sr.rs.next()) {
                FreezeUserBean bean = new FreezeUserBean();
                bean.id = sr.rs.getInt("id");
                bean.phone_number = sr.rs.getString("phone_number");
                bean.userId = sr.rs.getString("freeze_user_id");
                bean.freezeStatId = sr.rs.getString("freeze_stat_id");
                bean.reason = sr.rs.getString("reason");
                bean.free_time = sr.rs.getString("free_time");
                bean.create_time = sr.rs.getString("create_time");
                bean.update_time = sr.rs.getString("update_time");
                bean.enable = sr.rs.getBoolean("enable");

                return bean;
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }

        return null;
    }


}
